/*
 * @Descripttion : 折叠面板
 * @Author       : QianFox Team
 * @Date         : 2022-03-11 15:45:53
 * @LastEditors  : QianFox Team
 * @LastEditTime : 2022-06-01 08:51:24
 */

function Collapse() {
    this.HANDLE = '.foxui-collapse-handle:not(.is-disabled)';
    this.CONTAINER = '.foxui-collapse';
    this.ITEM = '.foxui-collapse-item';
    this.CONTENT = '.foxui-collapse-content';
    this.HEAD = '.foxui-collapse-head';
    this.HEAD_HANDLE = '.foxui-collapse-head.foxui-collapse-handle';
}

Collapse.prototype._init = function () {
    this._bindToggle();
};

Collapse.prototype._bindToggle = function () {
    let that = this;
    $(document).on('click', this.HANDLE, function () {
        let $this = $(this),
            $content = $this.closest(that.ITEM).children(that.CONTENT),
            $container = $this.closest(that.CONTAINER),
            $items = $container.children(that.ITEM),
            $headHandle = $items.children(that.HEAD_HANDLE),
            $head_handle = $items.children(that.HEAD).find(that.HANDLE),
            isActive = $this.is('.is-active'),
            isACcordion = $container.attr('data-fold') === 'accordion' ? true : false;

        if (isACcordion) {
            $headHandle.add($head_handle).each(function () {
                let $this = $(this);
                if ($this.is('.is-active')) {
                    $this.removeClass('is-active');
                    $this.closest(that.ITEM).children(that.CONTENT).slideUp('fast');
                }
            });
        }

        if (isActive) {
            $this.removeClass('is-active');
            $content.slideUp('fast');
        } else {
            $this.addClass('is-active');
            $content.slideDown('fast');
        }
    });
};

let collapse = new Collapse();

export { collapse };
